package com.example.index.web;

import com.example.index.bean.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class Index {
    @GetMapping("index")
    public String index(HttpServletRequest request){
        return "index: " + request.getServerPort();
    }

    @Resource
    RestTemplate restTemplate;

    @GetMapping("index_order")
    @HystrixCommand(fallbackMethod = "cloudOrder")
    public String order(){
        return restTemplate.getForObject(
                "http://order/order",String.class
        );
    }
    //降级方法
    public String cloudOrder(){
        return "服务熔断： 降级执行 cloudOrder";
    }

    @GetMapping("index_user")
    public String user(){
        return restTemplate.getForObject(
                "http://user/user",String.class
        );
    }

    @Resource
    ICloudOrderAction order;

    @GetMapping("feign_hello")
    public String hello(String name){
        return order.hello(name);
    }
    @GetMapping("feign_add")
    public int add(int a, int b){
        return order.add(a,b);
    }
    @GetMapping("feign_reg")
    public User reg(User user){
        return order.reg(user);
    }
    @GetMapping("feign_reg1")
    public User reg(User user, int age){
        return order.reg(user, age);
    }

    @GetMapping("Setsession")
    public String setSession(String name, HttpSession session){
        session.setAttribute("name", name);
        System.out.println("session.getId() = " + session.getId());
        return "name 已经在会话中";
    }
    @GetMapping("getsession")
    public String getSession(HttpSession session){
        System.out.println("session.getId() = " + session.getId());
        return "提取会话属性name： " + session.getAttribute("name");
    }


}
